package xin.yangshuai.jvm01.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.yangshuai.jvm01.reference.Hello;

import java.lang.ref.WeakReference;
import java.util.WeakHashMap;

/**
 * ReferenceController
 *
 * @author shuai
 * @date 2022/2/16
 */
@RestController
@RequestMapping("reference")
public class ReferenceController {

    WeakReference<Hello> helloWeakReference;

    WeakHashMap<String, String> map = new WeakHashMap<>();

    @RequestMapping("weakReference")
    public String weakReference() {
        helloWeakReference = new WeakReference<>(new Hello("123", 456));
        return "weakReference";
    }


    @RequestMapping("weakMap")
    public String weakMap() {

        map.put("a", "a");
        map.put("b", "b");
        map.put(new String("c"), "c");
        map.put(new String("d"), "d");

        return "weakMap";
    }

    @RequestMapping("info")
    public String info() {

        Hello hello = helloWeakReference.get();
        System.out.println(hello);

        String a = map.get("a");
        String b = map.get("b");
        String c = map.get("c");
        String d = map.get("d");
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);

        return "info";
    }

    @RequestMapping("gc")
    public String gc() {

        System.gc();

        return "gc";
    }


}
